/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.qb9.flashlib.tasks
{
	/** Abstract base class for all tasks that have a fixed duration. */
	public class TimeBasedTask extends Task
	{
		/** Task duration */
		public var duration:uint;

		public function TimeBasedTask(duration:uint)
		{
			this.duration = duration;
		}
		
		/** @inheritDoc */
		public override function clone():ITask
		{
			return new TimeBasedTask(duration);
		}

		/**
		 * Amount of time that must be elapsed until the animation finishes.
		 */
		public function get timeLeft():int
		{
			return duration - elapsed;
		}

		/** @inheritDoc */
		public override function update(milliseconds:uint):void
		{
			var consumed:uint = Math.min(milliseconds, this.timeLeft);
			super.update(consumed);
			updateState();
			if (elapsed == duration)
				this.taskComplete();
		}

		protected function updateState():void
		{
		}
	}
}
